package com.geovis.emergency.spd.controller.system;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.system.service.ISystemResourcesService;
import com.geovis.emergency.spd.entity.system.entity.SystemResources;
import com.geovis.emergency.spd.entity.system.entity.SystemRoleResources;
import com.geovis.emergency.spd.entity.system.pojo.constant.SystemConstant;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemResourcesDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemResourcesQueryDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemResourcesSaveDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemResourcesUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 权限管理 前端控制器
 * </p>
 *
 * @author 王响
 * @since 2020-07-27
 */
@RestController
@RequestMapping("/system/systemResources")
@AllArgsConstructor
@Api(value = "系统管理模块-权限管理相关接口", tags = "系统管理模块-权限管理相关接口")
@Slf4j
@Validated
public class SystemResourcesController extends BaseController<ISystemResourcesService> {

    @ApiOperation(value = "保存资源信息", notes = "保存资源信息")
    @PostMapping({"/save"})
    public Result save(@Validated @RequestBody SystemResourcesSaveDTO saveDTO) {
        Long count=baseService.count(Wrappers.lambdaUpdate(SystemResources.class).eq(SystemResources::getAuthName,saveDTO.getAuthName()));
        if(count>0)
        {
            return Result.failed("授权标识【"+saveDTO.getAuthName()+"】已存在，无法添加");
        }

        SystemResources systemResources = BeanUtil.toBean(saveDTO, SystemResources.class);

        baseService.save(systemResources);
        return Result.ok();
    }

    @ApiOperation(value = "修改资源信息", notes = "修改资源信息")
    @PostMapping({"/update"})
    public Result update(@Validated @RequestBody SystemResourcesUpdateDTO updateDTO) {
        SystemResources old=baseService.getById(updateDTO.getId());
        if(!old.getAuthName().equals(updateDTO.getAuthName()))
        {
            Long count=baseService.count(Wrappers.lambdaUpdate(SystemResources.class).eq(SystemResources::getAuthName,updateDTO.getAuthName()));
            if(count>0)
            {
                return Result.failed("授权标识【"+updateDTO.getAuthName()+"】已存在，无法添加");
            }
        }
        SystemResources systemResources = BeanUtil.toBean(updateDTO, SystemResources.class);
        baseService.updateById(systemResources);
        return Result.ok();
    }



    @ApiOperation(value = "查询资源信息", notes = "查询资源信息")
    @PostMapping("/list")
    public Result<List<SystemResources>> list(String name) {
        return Result.ok(baseService.list(Wrappers.lambdaUpdate(SystemResources.class).like(StringUtils.hasText(name),SystemResources::getName,name)));
    }

    @ApiOperation(value = "批量删除资源数据", notes = "批量删除资源数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        baseService.removeByIdList(idList);
        return Result.ok();
    }
}
